package itsm.isperp.module.controller.bpm;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppRole;
import itsm.isperp.module.entity.bpm.BpmNodeSetting;
import itsm.isperp.module.entity.bpm.BpmNodeUser;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.bpm.BpmNodeSettingService;
import itsm.isperp.module.service.bpm.BpmNodeUserService;
import itsm.isperp.module.service.db.DbEntityFieldService;
import itsm.isperp.workflow.graph.Node;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2013-7-3
 * 
 */
@Controller
@RequestMapping(value = "bpm/nodeUser")
public class BpmNodeUserController extends BaseController<BpmNodeUser> {

	@Autowired
	protected BpmNodeUserService bpmNodeUserService;

	@Autowired
	protected AppRoleService appRoleService;

	@Autowired
	protected DbEntityFieldService dbEntityFieldService;

	@Autowired
	BpmNodeSettingService bpmNodeSettingService;

	@Override
	public BpmNodeUserService getService() {
		return bpmNodeUserService;
	}

	@Override
	public String getPageTitle() {
		return "节点流程";
	}

	/**
	 * 获取实体列表的json格式数据
	 * 
	 */
	@Override
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<BpmNodeUser> list(JqGridRequest request,
			HttpServletRequest req) {

		String bpmNodeSettingId = req.getParameter("bpmNodeSettingId");

		return this
				.getService()
				.findAll(
						request.isPageable(false).addFilterRule(
								"bpm_node_setting_id", JqGridFilterRuleOp.eq,
								bpmNodeSettingId)).toPage();
	}
	
	

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		ModelAndView mv = this.getModelAndView();
		BpmNodeUser model = null;
		String processKey="";
		String bpmNodeSettingId = req.getParameter("bpmNodeSettingId");
		

		mv.addObject("all", req.getParameter("all"));

		if (StringUtils.isEmpty(id)) {
			processKey=this.bpmNodeSettingService.getProcessKeyByNodeId(bpmNodeSettingId);
			model = new BpmNodeUser();
			model.setBpmNodeSettingId(bpmNodeSettingId);
		} else {
			model = this.getService().findOne(id);
			bpmNodeSettingId = model.getBpmNodeSettingId();
			processKey=this.bpmNodeSettingService.getProcessKeyByNodeId(bpmNodeSettingId);
		}
		
		List<Node> nodeList=bpmNodeSettingService.getAllNodes(processKey);

		List<AppRole> roles = appRoleService.findAllRole("id", "name");

		// 先获取
		DbEntity dbEntity = bpmNodeSettingService
				.findDbEntityById(bpmNodeSettingId);
		// 获取表单中所有的字段
		if(dbEntity!=null)
		{
			List<DbEntityField> fields = dbEntityFieldService.findAll(
					new JqGridRequest()
							.isPageable(false)
							.setSelectColumns("id,name,column_name")
							.addFilterRule("db_entity_id", JqGridFilterRuleOp.eq,
									dbEntity.getId())).getContent();
			mv.addObject("fields", fields);
		}
		if(nodeList!=null)
		{
			mv.addObject("nodeList", nodeList);
		}
		mv.addObject("roles", roles);
		mv.addObject("model", model);
		return mv;
	}
}
